﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SalesSys.BLL;
using SalesSys.Comm.Utility;
using SalesSys.DAL;
using SalesSys.DAL.Base;
using SalesSys.DAL.Models;
using SalesSys.Tests.hll;
using SalesSys.Tests.Properties;
using SalesSys.WebCore;
using SalesSys.BLL.Sales;

namespace SalesSys.Tests
{
    [TestClass]
    public class DataInit
    {
        [TestMethod]
        public void CreatrByDrop()
        {
            var modelChange = new DropCreateDatabaseAlways<WXDbContext>();
            //modelBuilder.Conventions.Remove<IncludeMetadataConvention>()  移除数据表结构变化检查sql语句
            // Database.SetInitializer<WXDbContext>(null)   直接初始化数据库，不进行检查
            Database.SetInitializer(modelChange);
            var mt = new WXDbContext();
            //添加推广模块模型注册
            // WXDbContext.DbModelCreatingReg += p => PlanModelReg(p);

           
            //mt.Database.
            var objCtx = ((IObjectContextAdapter) mt).ObjectContext;
            // objCtx.ExecuteStoreCommand(initSeqSQL);
            var s = objCtx.CreateDatabaseScript();
            modelChange.InitializeDatabase(mt);
            Log.m.Debug(s);
            SqlDataInit();
          //  ConfigInit();
            InsertTest();
        }

        [TestMethod]
        public void CreateDbNotExists()
        {
            var modelCreate = new CreateDatabaseIfNotExists<WXDbContext>();
            Database.SetInitializer(modelCreate);
            var mt = new WXDbContext();
            modelCreate.InitializeDatabase(mt);
            var objCtx = ((IObjectContextAdapter) mt).ObjectContext;
            // objCtx.ExecuteStoreCommand(initSeqSQL);
            var s = objCtx.CreateDatabaseScript();
            Log.m.Debug(s);
        }
         [TestMethod]
        public void SqlDataInit()
        {
            System.Data.Entity.Database.SetInitializer<WXDbContext>(null);
            var db = new WXDbContext();
            var objCtx = ((IObjectContextAdapter)db).ObjectContext;
            // objCtx.ExecuteStoreCommand(initSeqSQL);
            var s = objCtx.CreateDatabaseScript();
            objCtx.ExecuteStoreCommand(Resources.TextFile1);
        }

        [TestMethod]
        public void InsertTest()
        {
            ApprovalTest.InitData();
            System.Data.Entity.Database.SetInitializer<WXDbContext>(null);
            var db = new WXDbContext();
            int showInd = 1;
            //
             var dep1 = new Department() { Name = "信息中心" };
             var dep2 = new Department() { Name = "运营中心" };
             var dep3 = new Department() { Name = "洛阳公司" };
             var dep4 = new Department() { Name = "郑州公司" };
             db.Set<Department>().AddRange(new[] { dep1, dep2, dep3, dep4 });
            var emp = new Employee
            {
                CreateTime = DateTime.Now,
                Department = dep1,
                IsConfirm = true,
                LoginName = "sa",
                LoginPwd = "123456",
                Name = "管理员",
                Roles = "A|1|2|3|4|5|6"
            };
            var emp2 = new Employee
            {
                CreateTime = DateTime.Now,
                Department = dep2,
                IsConfirm = true,
                LoginName = "test",
                LoginPwd = "123456",
                Name = "test用户",
                Roles = "1|2|3|4|5|6"
            };
            db.Set<Employee>().Add(emp);
            db.Set<Employee>().Add(emp2);
            //
            var dis = db.Set<District>().Find(Guid.Parse("4ab137c0-4ffc-4443-8f47-bfc9683a0001"));
            //
            var super = new Supplier();
            super.Name = "洛阳洛龙区_供应商" + showInd++;
            //super.IsDepot = true;
            //super.NonstandardDiscount = -6;
            //super.MainFieldMan = emp;
            super.District = dis;
            //super.FullPaymentDiscount = -6;
            super.BasePrice = 3000;
            var super2 = new Supplier();
            super2.Name = "中泰立恒" + showInd++;
            //super2.IsDepot = false;
            //super2.NonstandardDiscount = -5;
            //super2.MainFieldMan = emp2;
            super2.District = dis;
            //super2.FullPaymentDiscount = -5;
            super2.BasePrice = 3100;
    
             db.Set<Supplier>().AddRange(new[] { super, super2 });
          
            //
            var cm = new Customer
            {
                CreateTime = DateTime.Now,
                Name = "XXX公司" + showInd,
                ShortName = "X客户" + showInd++,
                ClientManager = emp,
                District = dis,
                Accounts = new List<Account>()
                {
                    new Account() {Name = "河南XXX股份有限公司"+showInd++},
                    new Account() {Name = "河南YYY股份有限公司"+showInd++}
                },

            };
            var cm2 = new Customer()
            {
                CreateTime = DateTime.Now,
                Name = "YYY公司",
                ShortName = "Y客户",
                ClientManager = emp2,
                District = dis,
                Accounts = new List<Account>()
                {
                    new Account() {Name = "洛阳XXX股份有限公司"+showInd++},
                    new Account() {Name = "洛阳YYY股份有限公司"+showInd++}
                }
            };
            db.Set<Customer>().AddRange(new[] { cm, cm2 });
             //户头
             var account = new Account()
             {
                 Customer = cm,
                 Amount = 10000,
                 BankName = "中国银行",
                 BankNumber = "6222222222222",
                 Name = "河南XXX股份有限公司工行" + showInd++,
                 Remark = "备注备注"
             };
             var account2 = new Account()
             {
                 Customer = cm,
                 Amount = 10000,
                 BankName = "中国银行",
                 BankNumber = "6222222222222",
                 Name = "河南XXX股份有限公司工行" + showInd++,
                 Remark = "备注备注"
             };
             db.Set<Account>().AddRange(new[] { account, account2 });


            //
            var cmmd = new Commodity
            {
                //Price = 3030,
                //PriceDifference = 30,
                Standard = "300E",
                Supplier = super,
                RecordTime = DateTime.Now,
                Creater = emp,
                Weight = 2,
                Name = "螺纹钢",
                //Amount = 80000000,
                Brand = "中泰",
                Format = "$10,11m"
            };
            var cmmd2 = new Commodity
            {
                //Price = 3130,
                //PriceDifference = 30,
                Standard = "300E",
                Supplier = super2,
                RecordTime = DateTime.Now,
                Creater = emp2,
                Weight = 2,
                Name = "螺纹钢",
                //Amount = 80000000,
                Brand = "中泰",
                Format = "$12,11m"
            };
            db.Set<Commodity>().Add(cmmd);
            db.Set<Commodity>().Add(cmmd2);
            var dCmmd = new DepotCommodity();
            dCmmd.Commodity = cmmd;
            dCmmd.Amount = 1000;
            dCmmd.Price = 3000;
            dCmmd.UpdateTime = DateTime.Now;
            var dCmmd2 = new DepotCommodity();
            dCmmd2.Commodity = cmmd2;
            dCmmd2.UpdateTime = DateTime.Now;
             //仓库
            Provider provider = new Provider();
            provider.Name = "洛阳test库";
            provider.MainFieldMan = emp;
            provider.Manager = emp2;
             provider.DepotCommoditys = new List<DepotCommodity>();
            provider.DepotCommoditys.Add(dCmmd);
            provider.NonstandardDiscountCase = SimpleJson.Serializer(new Dictionary<Guid, float>() {{super.Id,7},{super2.Id,8}});
            provider.Supplier = super;
         
             Provider depot2 = new Provider();
             depot2.Name = "洛阳test库2";
             depot2.MainFieldMan = emp;
             depot2.Manager = emp2;
             depot2.DepotCommoditys = new List<DepotCommodity>();
             depot2.DepotCommoditys.Add(dCmmd2);
             db.Set<Provider>().AddRange(new []{ provider, depot2 });
            //
            var dCmmd3 = new DepotCommodityRecord();
            dCmmd3.Provider = provider;
            dCmmd3.Commodity = cmmd;
            dCmmd3.Amount = 1000;
            dCmmd3.Price = 3000;
            dCmmd3.UpdateTime = DateTime.Now;
            var dCmmd4 = new DepotCommodityRecord();
            dCmmd4.Provider = provider;
            dCmmd4.Commodity = cmmd;
            dCmmd4.Amount = 1000;
            dCmmd4.Price = 3000;
            dCmmd4.UpdateTime = DateTime.Now;
            db.Set<DepotCommodityRecord>().Add(dCmmd3);
            db.Set<DepotCommodityRecord>().Add(dCmmd4);
            #region order
            
        
            var order = new Order()
            {
                CommodityInfos = new List<CommodityInfo>()
                {
                    new CommodityInfo
                    {
                         
                         Commodity = dCmmd.Commodity, Count=5, 
                        Records = new List<OrderRecord>()
                        {
                            new OrderRecord() {CreateTime = DateTime.Now, DiscountType = DiscountType.PriceChange,Remarks ="商品修改:价格3000"}
                        }
                    }
                },
                 Provider = provider,
                Creater = emp,
                Remarks = "测试订单",
                Customer = cm,
                 Account = account,
                District = dis,
                CreateTime = DateTime.Now
                //,Supplier = super
            };
            var order2 = new Order()
            {
                CommodityInfos = new List<CommodityInfo>()
                {
                    new CommodityInfo
                    {
                         Commodity = dCmmd.Commodity, Count=3,
                        Records = new List<OrderRecord>()
                        {
                                new OrderRecord() {CreateTime = DateTime.Now, DiscountType = DiscountType.PriceChange,Remarks ="商品修改:价格3100"}
                        }
                    }
                },
                 Provider = depot2,
                Creater = emp2,
                Remarks = "测试订单",
                 Customer = cm2,
                 Account = account2,
                District = dis,
                CreateTime = DateTime.Now
                //,Supplier = super
            };
            order.CommodityInfos.First().CalcNew();
            order2.CommodityInfos.First().CalcNew();
            order.CommodityInfos.First().Price = 3000;
            order2.CommodityInfos.First().Price = 3100;
            order.CalcNew();
            order2.CalcNew();
            
            db.Set<Order>().Add(order);
            db.Set<Order>().Add(order2);
            #endregion

            #region LockOrder

            var lockOrder = new LockOrder()
            {

                DepotCommodityRecords = new List<DepotCommodityRecord>()
                {
                   dCmmd3
                },
                Provider = provider,
                Creater = emp,
                Remarks = "测试订单",
                Customer = cm,
                Account = account,
                Total = 100000,
                IsGoodsLock = true,
                CreateTime = DateTime.Now,
                State = OrderState.Pending,
                ProcessState = ProcessState.Normal
            };
            var lockOrder2 = new LockOrder()
            {

                DepotCommodityRecords = new List<DepotCommodityRecord>()
                {
                   dCmmd4
                },
                Provider = depot2,
                Creater = emp2,
                Remarks = "测试订单2",
                Customer = cm2,
                Account = account2,
                Total = 100000,
                IsGoodsLock = true,
                CreateTime = DateTime.Now,
                State = OrderState.Pending,
                ProcessState = ProcessState.Normal
            };
            db.Set<LockOrder>().Add(lockOrder);
            db.Set<LockOrder>().Add(lockOrder2);
            #endregion

            #region Approval

            SequenceApprovalItem sappl = new SequenceApprovalItem();
            sappl.TypeName = SequenceApprovalBLL.BargainingApproval.ApprovalTypeName;
            sappl.Approver = emp;
            sappl.Index = 1;
            sappl.ApproverName = emp.Name;

            db.Set<SequenceApprovalItem>().Add(sappl);
            sappl = new SequenceApprovalItem();
            sappl.TypeName = SequenceApprovalBLL.BargainingApproval.ApprovalTypeName;
            sappl.Approver = emp2;
            sappl.ApproverName = emp2.Name;
            sappl.Index = 2;
         
            #endregion
            db.SaveChanges();
               db.Set<SequenceApprovalItem>().Add(sappl);
            var bargBll = new SequenceApprovalBLL.BargainingApproval();
            var orderBll=new OrderBLL();
           var onew= orderBll.Details(order.Id);
           bargBll.Submit(onew, 30);
        }

        [TestMethod]
        public void ConfigInit()
        {

            var s = new SysConfig() { Group = "产品类型", Value = "线材", Key = "线材" };
            var s1 = new SysConfig() { Group = "产品类型", Value = "螺纹钢", Key = "螺纹钢" };
            var s2 = new SysConfig() { Group = "产品类型", Value = "盘螺", Key = "盘螺" };
            var s3 = new SysConfig() { Group = "产品类型", Value = "钢胚", Key = "钢胚" };
            var st = new SysConfig() { Group = "规格", Key = "HPB300", Value = "HPB300", Value2 = "线材", Remarks = "Value代表显示名称,Value2代表所属产品类型key" };
            var st1 = new SysConfig() { Group = "规格", Key = "Q235", Value = "Q235", Value2 = "线材", Remarks = "Value代表显示名称,Value2代表所属产品类型key" };
            var st2 = new SysConfig() { Group = "规格", Key = "Q195", Value = "Q195", Value2 = "线材", Remarks = "Value代表显示名称,Value2代表所属产品类型key" };
            var st3 = new SysConfig() { Group = "规格", Key = "Q195L", Value = "Q195L", Value2 = "线材", Remarks = "Value代表显示名称,Value2代表所属产品类型key" };
            var st4 = new SysConfig() { Group = "规格", Key = "HRB335", Value = "HRB335", Value2 = "盘螺", Remarks = "Value代表显示名称,Value2代表所属产品类型key" };
            var st5 = new SysConfig() { Group = "规格", Key = "HRB400", Value = "HRB400", Value2 = "盘螺", Remarks = "Value代表显示名称,Value2代表所属产品类型key" };
            var st6 = new SysConfig() { Group = "规格", Key = "HRB400E", Value = "HRB400E", Value2 = "盘螺", Remarks = "Value代表显示名称,Value2代表所属产品类型key" };
            var st7 = new SysConfig() { Group = "规格", Key = "HRB500", Value = "HRB500", Value2 = "盘螺", Remarks = "Value代表显示名称,Value2代表所属产品类型key" };
            var st8 = new SysConfig() { Group = "规格", Key = "HRB500E", Value = "HRB500E", Value2 = "盘螺", Remarks = "Value代表显示名称,Value2代表所属产品类型key" };
            var st9 = new SysConfig() { Group = "规格", Key = "Q235", Value = "Q235", Value2 = "盘螺", Remarks = "Value代表显示名称,Value2代表所属产品类型key" };
            var st10 = new SysConfig() { Group = "规格", Key = "HRB400", Value = "HRB400", Value2 = "螺纹钢", Remarks = "Value代表显示名称,Value2代表所属产品类型key" };
            var st11 = new SysConfig() { Group = "规格", Key = "HRB400E", Value = "HRB400E", Value2 = "螺纹钢", Remarks = "Value代表显示名称,Value2代表所属产品类型key" };
            var st12 = new SysConfig() { Group = "规格", Key = "HRB500", Value = "HRB500", Value2 = "螺纹钢", Remarks = "Value代表显示名称,Value2代表所属产品类型key" };
            var st13 = new SysConfig() { Group = "规格", Key = "HRB500E", Value = "HRB500E", Value2 = "螺纹钢", Remarks = "Value代表显示名称,Value2代表所属产品类型key" };

            var amount = new SysConfig() { Group = "库存概要显示", Key = "1", Value = "0", Value2 = "无货", Remarks = "库存给客户经理显示的文字" };
            var amount1 = new SysConfig() { Group = "库存概要显示", Key = "2", Value = "20", Value2 = "少量", Remarks = "库存给客户经理显示的文字" };
            var amount2 = new SysConfig() { Group = "库存概要显示", Key = "3", Value = "50", Value2 = "充足", Remarks = "库存给客户经理显示的文字" };


            var account1 = new SysConfig() { Group = "公司公户", Key = "物联建设银行户", Value = "物联建设银行户", Value2 = "物联建设银行户", Remarks = "公司公户信息" };
            var account2 = new SysConfig() { Group = "公司公户", Key = "莫凡交通银行户", Value = "莫凡交通银行户", Value2 = "莫凡交通银行户", Remarks = "公司公户信息" };

            //var tops = new Tuple<string, string[]>[4];
            //var amountDisplay = new Tuple<int, string>[3];
            System.Data.Entity.Database.SetInitializer<WXDbContext>(null);
            var db = new WXDbContext();
            db.Set<SysConfig>().AddRange(new[] { s, s1, s2, s3, st, st1, st2, st3, st4, st5, st6, st7, st8, st9, st10, st11, st12, st13, amount, amount1, amount2, account1, account2 });
            db.SaveChanges();
        }


    }
}
